<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: action.user.php Sat Jul 18 12:47:13 CST 2009
 */

include "../../../mainfile.php" ;

$group_id = isset($_POST["group_id"]) ? intval($_POST["group_id"]) : 0;

$ac = isset($_POST["ac"]) ? $_POST["ac"] : "";
xoops_loadLanguage("user","groups");

if ( empty($xoopsUser) || !is_object($xoopsUser)) {
    redirect_header("../index.php",5,_NOPERM);
}

$uid = $xoopsUser->getVar("uid");
/** 加载相关的类 **/
$groups_handler = xoops_getmodulehandler("groups","groups");
$category_handler = xoops_getmodulehandler("category","groups");
$column_handler = xoops_getmodulehandler("column","groups");
$members_handler = xoops_getmodulehandler("members","groups");
$notice_handler = xoops_getmodulehandler("notice","groups");
$events_handler = xoops_getmodulehandler("events","groups");
$posts_handler = xoops_getmodulehandler("posts","groups");
$threads_handler = xoops_getmodulehandler("threads","groups");
$footprint_handler = xoops_getmodulehandler("footprint","groups");
$user_handler = xoops_gethandler("member");

/** 权限检查 **/
$group_obj = $groups_handler->get($group_id);
if ( $ac != "delevent" && $ac != "delpost" ) {
    if ( $ac == "noperm" || !is_object($group_obj) || $group_obj->isNew() || $group_obj->getVar("uid") != $xoopsUser->getVar("uid") ) {
        $ac = "noperm";
    }
} else {
    if ( false === getIsGroupsAdmin($group_id,$xoopsUser->getVar("uid")) ) {
		$op = "noperm";
	}
}

switch ($ac) {
    /** 删除主题 **/
    case "delpost":
        $post_id = isset($_POST["post_id"]) ? intval($_POST["post_id"]) : 0;
        $post_obj = $posts_handler->get($post_id);
        $tid = $post_obj->getVar("tid");
        if (!$GLOBALS["xoopsSecurity"]->check()) {
            redirect_header("../viewthread.php?tid={$tid}", 3, implode("<br />", $GLOBALS["xoopsSecurity"]->getErrors()));
        }
        if ( !is_object($post_obj) || $post_obj->isNew() ) {
            redirect_header("../viewthread.php?tid={$post_obj->getVar("tid")}");
        }
        if ( $post_obj->getVar("first") ) {
            $threads_handler = xoops_getmodulehandler("threads","groups");
            if ( $threads_handler->delThread($tid) ) {
                $criteria = new CriteriaCompo(new Criteria("tid",$tid));
                $_post_ids = $posts_handler->getIds($criteria);
                if ( $_post_ids ) {
                    foreach ( $_post_ids as $k=>$_post_id ) {
                        $posts_handler->delPost($_post_id);
                    }
                }
                $_num = count($_post_ids);
                $groups_handler->changeTopics($group_id,-1,-1*$_num);
                $stop = _US_SUCCEED;
            } else {
                $stop = _US_FAILED;
            }
            redirect_header("../groups.php?op=forum&gid={$group_id}", 5, $stop);
        } else {
            if ( $posts_handler->delPost($post_id) ) {
                $groups_handler->changeTopics($group_id,0,-1);
                $stop = _US_SUCCEED;
            } else {
                $stop = _US_FAILED;
            }
        }
        redirect_header("../viewthread.php?tid={$tid}", 5, $stop);
    break;
    
    /** 执行删除事件 **/
    case "delevent":
        $event_id = isset($_POST["event_id"]) ? intval($_POST["event_id"]) : 0;
        if (!$GLOBALS["xoopsSecurity"]->check()) {
            redirect_header("groups.php?op=event&gid={$group_id}", 3, implode("<br />", $GLOBALS["xoopsSecurity"]->getErrors()));
        }
        if ( $events_handler->delEvent($event_id,$group_id) ) {
           $stop = _US_SUCCEED;
        } else {
            $stop = _US_FAILED;
        }
        redirect_header("../groups.php?op=event&gid={$group_id}", 5, $stop);
    break;
    
    /** 群成员 管理员操作 */
    case "removemember":
        $member_id = isset($_POST["member_id"]) ? intval($_POST["member_id"]) : 0;
        if (!$GLOBALS["xoopsSecurity"]->check()) {
            redirect_header("../admin.php?fct=member&gid={$group_id}", 3, implode("<br />", $GLOBALS["xoopsSecurity"]->getErrors()));
        }
        $_obj = $members_handler->get($member_id);
        if ( $members_handler->delGroupMember($member_id,$group_obj) ) {
            /** 产生一个事件 **/
            $_itemid = $_obj->getVar("uid");
            $_user = $user_handler->getUser($_itemid);
            $_subject = $_user->name();
            $events_handler->eventApi($uid,$_itemid,203,$_subject,$group_id,"");
            $stop = _US_SUCCEED;
        } else {
            $stop = _US_FAILED;
        }
        redirect_header("../admin.php?fct=member&gid={$group_id}", 5, $stop);
    break;
    
    case "passmember":
        $member_id = isset($_POST["member_id"]) ? intval($_POST["member_id"]) : 0;
        if (!$GLOBALS["xoopsSecurity"]->check()) {
            redirect_header("../admin.php?fct=member&gid={$group_id}", 3, implode("<br />", $GLOBALS["xoopsSecurity"]->getErrors()));
        }
        if ( $members_handler->passGroupMember($member_id,$group_id) ) {
            /** 产生一个事件 **/
            $_obj = $members_handler->get($member_id);
            $_itemid = $_obj->getVar("uid");
            $_user = $user_handler->getUser($_itemid);
            $_subject = $_user->name();
            $events_handler->eventApi($uid,$_itemid,204,$_subject,$group_id,"");
            
            $stop = _US_SUCCEED;
        } else {
            $stop = _US_FAILED;
        }
        redirect_header("../admin.php?fct=member&gid={$group_id}", 5, $stop);
    break;
    
    case "opadmin":
        $member_id = isset($_POST["member_id"]) ? intval($_POST["member_id"]) : 0;
        $isadmin = isset($_POST["isadmin"]) ? intval($_POST["isadmin"]) : 0;
        if (!$GLOBALS["xoopsSecurity"]->check()) {
            redirect_header("../admin.php?fct=member&gid={$group_id}", 3, implode("<br />", $GLOBALS["xoopsSecurity"]->getErrors()));
        }
        if ( $members_handler->addGroupAdmin($member_id,$isadmin) ) {
            /** 产生一个事件 **/
            $_type = !empty($isadmin) ? 205 : 206;
            $_obj = $members_handler->get($member_id);
            $_itemid = $_obj->getVar("uid");
            $_user = $user_handler->getUser($_itemid);
            $_subject = $_user->name();
            $events_handler->eventApi($uid,$_itemid,$_type,$_subject,$group_id,"");
            $stop = _US_SUCCEED;
        } else {
            $stop = _US_FAILED;
        }
        redirect_header("../admin.php?fct=member&gid={$group_id}", 5, $stop);
    break;
    
    /** 创建版块 **/
    case "savecolumn":
        $column_id = isset($_POST["column_id"]) ? intval($_POST["column_id"]) : 0;
        $column_name = isset($_POST["column_name"]) ? trim($_POST["column_name"]) : 0;
	    if ( empty($column_name) ) redirect_header("../admin.php?fct=column&gid={$group_id}", 5, _US_FORM_DESC);
	    
        if (!$GLOBALS["xoopsSecurity"]->check()) {
            redirect_header("../admin.php?fct=column&gid={$group_id}", 3, implode("<br />", $GLOBALS["xoopsSecurity"]->getErrors()));
        }
        
        if ( $column_id = $column_handler->setColumn($column_id,$group_id,$column_name) ) {
            /** 产生一个事件 **/
		    $events_handler->eventApi($uid,$column_id,216,$column_name,$group_id,"");
            $stop = _US_SUCCEED;
        } else {
            $stop = _US_FAILED;
        }
        redirect_header("../admin.php?fct=column&gid={$group_id}", 5, $stop);
    break;
    
    /** 解散群组 **/
    case "removegroup":
        $stop = _US_FAILED;
        $_obj = $group_obj;
        if ( $groups_handler->delete($group_obj) ) {
            $category_handler->changeGroups($_obj->getVar("cat_id"),-1);
            
            /** TODO **/
            $criteria = new CriteriaCompo(new Criteria("group_id",$group_id));
            $members_handler->deleteAll($criteria);
            $notice_handler->deleteAll($criteria);
            $footprint_handler->deleteAll($criteria);
            $events_handler->deleteAll($criteria);
            $threads_handler->updateAll("group_id",0,$criteria);
            $posts_handler->updateAll("group_id",0,$criteria);
            $column_handler->updateAll("group_id",0,$criteria);
            $stop = _US_SUCCEED;
        }
        redirect_header("../index.php", 5, $stop);
    break;
    
	/** 修改群资料 **/
	case "savegroup":
	    $cat_id = isset($_POST["cat_id"]) ? intval($_POST["cat_id"]) : 0;
	    $group_name = isset($_POST["group_name"]) ? trim($_POST["group_name"]) : "";
	    $group_description = isset($_POST["group_description"]) ? trim($_POST["group_description"]) : "";
	    $group_notice = isset($_POST["group_notice"]) ? trim($_POST["group_notice"]) : "";
	    $isnew = $group_obj->isNew();
	    if ( empty($isnew) ) {
	        if( $group_obj->getVar("uid") != $uid ) {
	            redirect_header("../groups.php?gid={$group_id}",5,_NOPERM);
	        }
	    }
	    
	    $status = $group_obj->getVar("group_status");
	    if ( $cat_id != $group_obj->getVar("cat_id") ) {
		    $status = $category_handler->getCateToGroupStatus($cat_id);
		}
	    
		if (!$GLOBALS["xoopsSecurity"]->check()) {
            redirect_header("../admin.php?gid={$group_id}", 3, implode("<br />", $GLOBALS["xoopsSecurity"]->getErrors()));
        }
        
		if ( $group_id = $groups_handler->setGroup($group_id,$cat_id,$uid,$group_name,$group_description,$status,$group_notice) ) {
		    if ( $isnew ) {
    		    $members_handler->addGroupmember($group_id,$uid,1);
		    }
		    
		    if ( $group_description != $group_obj->getVar("group_description") ) {
    		    /** 产生一个事件 **/
    		    $events_handler->eventApi($uid,$group_id,200,$group_name,$group_id,$group_description);
		    }
		    
		    /** 如果修改分类，分类数量要修改 **/
		    if ( $cat_id != $group_obj->getVar("cat_id") ) {
		        $category_handler->changeGroups($cat_id,1);
		        $category_handler->changeGroups($group_obj->getVar("cat_id"),-1);
		    }
		    /** 存在logo 就上传 **/
		    if ( $_FILES[$_POST["xoops_upload_file"][0]]["size"] > 0 ) {
		        include_once(dirname(__FILE__)."/uploader.php");
		        $uploadsDir = filesUploader::mkUploadsDir("groups","logo");
		        $uploadObj = filesUploader::uploadFile($uploadsDir["image"]["path"],0);
	            $thumbs = filesUploader::setImageThumb($uploadsDir["image"]["path"],$uploadObj->getSavedFileName(),$uploadsDir["thumb"]["path"],$uploadObj->getSavedFileName(),array("45","45"));
	            if ( $thumbs ) { 
    	            $logo = $uploadsDir["thumb"]["dirname"] . "/" . $uploadObj->getSavedFileName();
    	            if ( $groups_handler->uploadGroupLogo($group_id,$logo) ) {
    	                if ( $group_obj->getVar("group_logo") != "group.gif" ) {
    	                   @unlink(XOOPS_UPLOAD_PATH."/{$group_obj->getVar("group_logo")}");
    	                }
    	            }
	            }
		    }
			$stop = _US_SUCCEED;
		} else {
			$stop = _US_FAILED;
		}
		redirect_header("../admin.php?gid={$group_id}",5,$stop);
	break;
		
	case "savegroupperm":
	    $group_access_permission = isset($_POST["group_access_permission"]) ? intval($_POST["group_access_permission"]) : $group_create_default_perm["group_access_permission"];
	    $group_post_permission = isset($_POST["group_post_permission"]) ? intval($_POST["group_post_permission"]) : $group_create_default_perm["group_post_permission"];
	    $group_reply_permission = isset($_POST["group_reply_permission"]) ? intval($_POST["group_reply_permission"]) : $group_create_default_perm["group_reply_permission"];
	    $group_join_permission = isset($_POST["group_join_permission"]) ? intval($_POST["group_join_permission"]) : $group_create_default_perm["group_join_permission"];
	    $uid = $xoopsUser->getVar("uid");
        if ( $group_id = $groups_handler->setGroupPerm($group_id,$group_access_permission,$group_post_permission,$group_reply_permission,$group_join_permission) ) {
			/** 产生一个事件 **/
		    $events_handler->eventApi($uid,$group_id,201,$group_obj->getVar("group_name","n"),$group_id,"");
            $stop = _US_SUCCEED;
		} else {
			$stop = _US_FAILED;
		}
		redirect_header("../admin.php?fct=perm&gid={$group_id}",5,$stop);
	break;
	
	default:
	    redirect_header("../index.php");
	break;
}